package ru.urfc.portal.rest;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import ru.urfc.portal.dao.BasePersistence;
import ru.urfc.portal.dao.RadioDao;
import ru.urfc.portal.dao.filter.Filter;
import ru.urfc.portal.managers.DeviceManager;
import ru.urfc.portal.managers.DocumentManager;
import ru.urfc.portal.model.documents.Document;
import ru.urfc.portal.model.radio.DeviceCertificate;
import ru.urfc.portal.model.radio.DeviceMeasurement;
import ru.urfc.portal.model.radio.DeviceSector;
import ru.urfc.portal.model.radio.DeviceStateChange;
import ru.urfc.portal.model.radio.DeviceWithExtra;
import ru.urfc.portal.model.radio.DeviceWithNet;
import ru.urfc.portal.model.radio.NetWithLicense;
import ru.urfc.portal.ui.client.Constants;

@Stateless
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Path(Constants.CLIENTS + "/{clientId}/" + Constants.DEVICES)
public class DeviceService extends BaseService {
	
	@EJB BasePersistence basePersistence;
	@EJB RadioDao deviceDao;	
	@EJB DeviceManager deviceManager;	
	@EJB DocumentManager documentManager;	
	
	private static final String CLIENT_ID = "clientId";  
	private static final String DEVICE_ID = "deviceId";  

	@GET
	public List<DeviceWithNet> getDevices(@PathParam(CLIENT_ID) Long clientId, @Context HttpServletRequest request) {
		Long stateCode = getLong(FILTER_DEVICE_STATE_CODE, request);
		Long netId = getLong(FILTER_DEVICE_NET_ID, request);
		return deviceManager.getDevices(clientId, new Filter("state.code", stateCode).addEqual("net.id", netId));
	}


	@Path("{" + DEVICE_ID + "}")
	public SubService getSubService() {
		return new SubService(); 
	}
	
	public class SubService {
	
		@GET
		public DeviceWithExtra getDevice(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDevice(clientId, deviceId);
		}		
		
		@GET
		@Path(DEVICE_NET)
		public NetWithLicense getNet(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDeviceNet(clientId, deviceId);
		}
		
		@GET
		@Path(DEVICE_SECTORS)
		public List<DeviceSector> getSectors(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDeviceSectors(clientId, deviceId);
		}
	
		@GET
		@Path(DEVICE_CRETIFICATES)
		public List<DeviceCertificate> getCertificates(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDeviceCertificates(clientId, deviceId);
		}
		
		@GET
		@Path(DEVICE_MEASURENENTS)
		public List<DeviceMeasurement> getDeviceMeasurements(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDeviceMeasurements(clientId, deviceId);
		}
		
		@GET
		@Path(DEVICE_STATE_CHANGES)
		public List<DeviceStateChange> listStateHistory(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return deviceManager.getDeviceStateChanges(clientId, deviceId); 
		}
	
		//TODO
		@GET
		@Path(DEVICE_DOCUMENTS)
		public List<Document> getDeviceDocuments(@PathParam(CLIENT_ID) Long clientId, @PathParam(DEVICE_ID) Long deviceId) {
			return documentManager.getDeviceDocuments(clientId, deviceId);
		}
	}
	
	
}
